
<?php 
// Nguyen Binh
// suongmumc@gmail.com
// update 05/2011

defined( '_VALID_NVB' ) or die( 'Direct Access to this location is not allowed.' );
$id_catpd=intval($_REQUEST['id_product_filter']);
$id=intval($_REQUEST['id']);
$id_filter_product=$_GET['id_filter_product'];
// add new
if($_POST['code']=='addnew'){
	$lable_filter_cat=clean_value($_POST['lable_filter_cat']);
	$order_filter_cat=intval($_POST['order_filter_cat']);
	$sql1="INSERT INTO filter(name,thu_tu,id_catpd,parentid) VALUES('".$lable_filter_cat."','".$order_filter_cat."','".$id_catpd."','0')";
	$DB->query($sql1);
	$idcat=intval(mysql_insert_id());
	$ncount=intval($_POST['ncount']);
	$str='';
	$i=1;
	for($i==1; $i<=$ncount; $i++){
		if($_POST['lable_filter_'.$i]!=''){
			$str="INSERT INTO filter(name,thu_tu,id_catpd,parentid) VALUES('".clean_value($_POST['lable_filter_'.$i])."','".intval($_POST['order_filter_'.$i])."','".$id_catpd."','".$idcat."')"."; ";
			$DB->query($str);
		}
		
	}
	list_filter($id_catpd);
}elseif(!$_REQUEST['code']){
	list_filter($id_catpd);
}
// delete
if($_GET['code']=='delete'){
	$sql1="SELECT * FROM filter WHERE id_filter=$id";
	$db1=$DB->query($sql1);
	if($rs1=mysql_fetch_array($db1)){
		if($rs1['parentid']==0){
			$sql2="SELECT * FROM filter WHERE parentid=$rs1[id_filter]";
			$db2=$DB->query($sql2);
			$list_id=0;
			while($rs2=mysql_fetch_array($db2)){
				$list_id.=",".intval($rs2['id_filter']);	
			}
			$sqlx="DELETE FROM filter WHERE id_filter IN(".$list_id.")";
			$DB->query($sqlx);
		}
	}
	$DB->query("DELETE FROM filter WHERE id_filter=".$id);
	list_filter($id_catpd);	
}
// load filter
if($_GET['code']=='load_filter'){
	load_filter($id_catpd,$id_filter_product);
}

// update
if($_GET['code']=='update'){
	$tpl=new TemplatePower("skin/filterform.htm");
	$tpl->prepare();
	
	$sql="SELECT * FROM filter WHERE id_filter=$id";
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		$tpl->assign("namecat",$rs['name']);
		$tpl->assign("cat_thu_tu",$rs['thu_tu']);
		$sql1="SELECT * FROM filter WHERE parentid=$rs[id_filter] ORDER BY thu_tu ASC, name ASC"	;
		$db1=$DB->query($sql1);
		$i=0;
		while($rs1=mysql_fetch_array($db1)){
			$i++;
			$tpl->newBlock("list_filter");
			$tpl->assign("name",$rs1['name']);	
			$tpl->assign("thu_tu",$rs1['thu_tu']);	
			$tpl->assign("i",$i);
		}
	}
	$tpl->printToScreen();
}


function list_filter($id_catpd){
	global $DB;
	$sql1="SELECT * FROM filter WHERE parentid=0 AND id_catpd=$id_catpd ORDER BY thu_tu DESC, name ASC";
	$db1=$DB->query($sql1);
	while($rs1=mysql_fetch_array($db1)){
		$str='<ul class="ulfilter"><li class="cat">'.$rs1['name'].'<a href="#item_filter_'.$rs1['id_filter'].'" onclick="delete_filter(\''.$rs1['id_filter'].'\'); return false;"><div class="thungrac"></div></a><a href="#item_filter_'.$rs1['id_filter'].'" onclick="load_filter_update(\''.$rs1['id_filter'].'\'); return false;"><div class="edittool"></div></a></li>';
		$sql="SELECT * FROM filter WHERE id_catpd=$id_catpd AND parentid=$rs1[id_filter] ORDER BY thu_tu DESC, name ASC";
		$db=$DB->query($sql);
		while($rs=mysql_fetch_array($db)){
			$str.='<li id="item_filter_'.$rs['id_filter'].'">'.$rs['name'].'<a href="#item_filter_'.$rs['id_filter'].'" onclick="delete_filter(\''.$rs['id_filter'].'\'); return false;"><div class="thungrac"></div></a></li>';	
		}
		echo $str."</ul>";
	}
}
/*function load_filter($id_catpd){
	global $DB;
	$sql1="SELECT * FROM filter WHERE parentid=0 AND id_catpd=$id_catpd ORDER BY thu_tu DESC, name ASC";
	$db1=$DB->query($sql1);
	while($rs1=mysql_fetch_array($db1)){
		$str='<ul class="load_filter"><li class="cat">'.$rs1['name'].'</li>';
		$sql="SELECT * FROM filter WHERE  parentid=$rs1[id_filter] ORDER BY thu_tu DESC, name ASC";
		$db=$DB->query($sql);
		while($rs=mysql_fetch_array($db)){
			$str.='<li id="item_filter_'.$rs['id_filter'].'"><input name="id_filter[]" type="checkbox" value="'.$rs['id_filter'].'" id="id_checkbox_filter_'.$rs['id_filter'].'" /><label for="id_checkbox_filter_'.$rs['id_filter'].'">'.$rs['name'].'</label></li>';	
		}
		echo $str."</ul>";
	}
}
*/
function load_filter($id_catpd,$filter){
	global $DB;
	$fil=array();
	$fil=explode(":",$filter);
	$sql1="SELECT * FROM filter WHERE parentid=0 AND id_catpd=$id_catpd ORDER BY thu_tu DESC, name ASC";
	$db1=$DB->query($sql1);
	while($rs1=mysql_fetch_array($db1)){
		$str.='<ul class="load_filter"><li class="cat">'.$rs1['name'].'</li>';
		$sql="SELECT * FROM filter WHERE id_catpd=$id_catpd AND parentid=$rs1[id_filter] ORDER BY thu_tu DESC, name ASC";
		$db=$DB->query($sql);
		while($rs=mysql_fetch_array($db)){
			if(in_array($rs['id_filter'],$fil)){
				$str.='<li id="item_filter_'.$rs['id_filter'].'"><input name="id_filter[]" type="checkbox" value="'.$rs['id_filter'].'" id="id_checkbox_filter_'.$rs['id_filter'].'" checked="checked"/><label for="id_checkbox_filter_'.$rs['id_filter'].'">'.$rs['name'].'</label></li>';	
			}else{
				$str.='<li id="item_filter_'.$rs['id_filter'].'"><input name="id_filter[]" type="checkbox" value="'.$rs['id_filter'].'" id="id_checkbox_filter_'.$rs['id_filter'].'"  /><label for="id_checkbox_filter_'.$rs['id_filter'].'">'.$rs['name'].'</label></li>';	
			}
		}
		$str.="</ul>";
		
	}
	echo  $str;

}
?>
